package com.keyroy.game.puzzle_rpg.models.attribute;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.keyroy.util.tag.Tag;

import com.keyroy.game.puzzle_rpg.models.item.Armour;
import com.keyroy.game.puzzle_rpg.models.item.Glove;
import com.keyroy.game.puzzle_rpg.models.item.Gold;
import com.keyroy.game.puzzle_rpg.models.item.Hat;
import com.keyroy.game.puzzle_rpg.models.item.Item;
import com.keyroy.game.puzzle_rpg.models.item.Necklace;
import com.keyroy.game.puzzle_rpg.models.item.QuestItem;
import com.keyroy.game.puzzle_rpg.models.item.Ring;
import com.keyroy.game.puzzle_rpg.models.item.Shield;
import com.keyroy.game.puzzle_rpg.models.item.Shoe;
import com.keyroy.game.puzzle_rpg.models.item.SpentItem;
import com.keyroy.game.puzzle_rpg.models.item.Weapon;

public class AttributeUtil {
	private static Hashtable<String, Class<? extends Attribute>> attributeTable = new Hashtable<String, Class<? extends Attribute>>();

	public static final List<Class<? extends Param>> PARAM_LIST = new ArrayList<Class<? extends Param>>();
	public static final List<Class<? extends Item>> ITEM_LIST = new ArrayList<Class<? extends Item>>();

	static {
		PARAM_LIST.add(Strength.class);
		PARAM_LIST.add(Dexterity.class);
		PARAM_LIST.add(Knowledge.class);
		PARAM_LIST.add(Vitality.class);
		PARAM_LIST.add(PhysicsDefense.class);
		PARAM_LIST.add(PhysicsDamage.class);
		PARAM_LIST.add(MagicDefense.class);
		PARAM_LIST.add(MagicDamage.class);
		PARAM_LIST.add(Sleep.class);
		PARAM_LIST.add(Speed.class);
		PARAM_LIST.add(HP.class);
		PARAM_LIST.add(Experience.class);
		PARAM_LIST.add(Gold.class);
		PARAM_LIST.add(Level.class);
		PARAM_LIST.add(ParamPoint.class);
		PARAM_LIST.add(RandomParam.class);

		ITEM_LIST.add(Hat.class);
		ITEM_LIST.add(Necklace.class);
		ITEM_LIST.add(Glove.class);
		ITEM_LIST.add(Ring.class);
		ITEM_LIST.add(Armour.class);
		ITEM_LIST.add(Shoe.class);
		ITEM_LIST.add(Weapon.class);
		ITEM_LIST.add(Shield.class);
		ITEM_LIST.add(SpentItem.class);
		ITEM_LIST.add(QuestItem.class);

		for (int i = 0; i < PARAM_LIST.size(); i++) {
			Class<? extends Attribute> clazz = PARAM_LIST.get(i);
			try {
				Attribute obj = clazz.newInstance();
				attributeTable.put(obj.getTagName(), clazz);
			} catch (Exception e) {
			}
		}

		for (int i = 0; i < ITEM_LIST.size(); i++) {
			Class<? extends Attribute> clazz = ITEM_LIST.get(i);
			try {
				Attribute obj = clazz.newInstance();
				attributeTable.put(obj.getTagName(), clazz);
			} catch (Exception e) {
			}
		}
	}

	public static List<Attribute> getAttributes(Tag tag) {
		List<Attribute> attributes = new ArrayList<Attribute>(tag.getChildTagArray().size());
		List<Tag> tags = tag.getChildTagArray();
		for (int i = 0; i < tags.size(); i++) {
			Tag cTag = tags.get(i);
			Class<? extends Attribute> clazz = attributeTable.get(cTag.getName());
			if (clazz != null) {
				try {
					Attribute attribute = clazz.newInstance();
					attribute.initObj(cTag);
					attributes.add(attribute);
				} catch (Exception e) {
				}
			} else {
				System.out.println("AttributeUtil.getAttributes(Tag tag) : can not find class " + tag.getName());
			}
		}
		return attributes;
	}

	public static Class<? extends Item> getItemClass(int index) {
		return ITEM_LIST.get(index);
	}

	public static String[] getItemTypes() {
		String[] strs = new String[ITEM_LIST.size()];
		for (int i = 0; i < ITEM_LIST.size(); i++) {
			try {
				Item attribute = ITEM_LIST.get(i).newInstance();
				strs[i] = attribute.getTypeName();
			} catch (Exception e) {
			}
		}
		return strs;
	}

	public static int getItemIndex(Class<? extends Item> clazz) {
		return ITEM_LIST.indexOf(clazz);
	}

	public static String[] getParamTypes() {
		String[] strs = new String[PARAM_LIST.size()];
		for (int i = 0; i < PARAM_LIST.size(); i++) {
			try {
				Attribute attribute = PARAM_LIST.get(i).newInstance();
				strs[i] = attribute.getTypeName();
			} catch (Exception e) {
			}

		}
		return strs;
	}

	public static String[] getParamTypes(boolean containRandomParam) {
		String[] strs = null;
		if (containRandomParam) {
			strs = new String[PARAM_LIST.size()];
		} else {
			strs = new String[PARAM_LIST.size() - 1];
		}

		for (int i = 0; i < strs.length; i++) {
			try {
				Attribute attribute = PARAM_LIST.get(i).newInstance();
				strs[i] = attribute.getTypeName();
			} catch (Exception e) {
			}
		}
		return strs;
	}

	public static int getParamIndex(Class<? extends Param> clazz) {
		return PARAM_LIST.indexOf(clazz);
	}

	public static Class<? extends Param> getParamClass(int index) {
		return PARAM_LIST.get(index);
	}
}
